'''
$Revision: 62 $
$LastChangedDate: 2008-03-13 18:10:27 +0000 (Thu, 13 Mar 2008) $
Testing delta dict
'''

import unittest
import os, sys

from MomentClosure.GetReactionInformation import GetStoichiometryDict
from MomentClosure.VariableDict import Variables
from swiginac import numeric

class TestReactionParsing(unittest.TestCase):
    
    
    def setUp(self):
        pass

    def testImmigrationDeath(self):
        self.__testImmigrationDeath('xml_files/imdeath.xml')
        #With local parameter
        self.__testImmigrationDeath('xml_files/smfsb/ch05-imdeath.xml')
    
    
    def __testImmigrationDeath(self, file_name):
        Variables().initialise()
        gdd = GetStoichiometryDict(sbml_file = file_name)
        reaction_list = gdd.getReactionList()
        
        l = Variables().getParameter('lambda')
        m = Variables().getParameter('mu')
        for value in reaction_list:
            if value.sbml_id == 'Immigration':
                self.assertEqual([1], value.stoic)
                self.assertEqual( {(0,): l}, value.rate)
            elif value.sbml_id == 'Death':
                self.assertEqual([-1], value.stoic)
                self.assertEqual( {(1,): m}, value.rate)
            else:
                print value.sbml_id
                raise 'Unknown reaction'


 
    def testImmigrationDeathMigration(self):
        Variables().initialise()
        gdd = GetStoichiometryDict(sbml_file = 'xml_files/immigration_migration_model.xml')
        reaction_list = gdd.getReactionList()
        
        mu2 = Variables().getParameter('mu2')
        k21 = Variables().getParameter('k21')
        I1  = Variables().getParameter('I1')

        for value in reaction_list:
            if value.sbml_id == 'Y_death':
                self.assertEqual([0, -1], value.stoic)
                self.assertEqual({(0, 1): mu2}, value.rate)
            elif value.sbml_id == 'X_death':
                self.assertEqual([-1, 1], value.stoic)
                self.assertEqual({(1, 0): k21}, value.rate)
            elif value.sbml_id == 'immigration':
                self.assertEqual([1, 0], value.stoic)
                self.assertEqual({(0, 0): I1}, value.rate)
            else:
                raise 'Unknown reaction'
    
  
    def testSISModel(self):
        Variables().initialise()
        gdd = GetStoichiometryDict(sbml_file = 'xml_files/sis.xml')
        reaction_list = gdd.getReactionList()
        
        alpha =Variables().getParameter('alpha')
        mu = Variables().getParameter('mu')
        beta = Variables().getParameter('beta')
        delta = Variables().getParameter('delta')

        for value in reaction_list:

            if value.sbml_id == 'Y_death':
                self.assertEqual([0, -1], value.stoic)
                self.assertEqual({(0, 1): mu}, value.rate)
            elif value.sbml_id == 'infection':
                self.assertEqual([-1, 1], value.stoic)
                self.assertEqual({(1, 1): beta}, value.rate)
            elif value.sbml_id == 'Y_imm':
                self.assertEqual([0, 1], value.stoic)
                self.assertEqual({(0, 0): delta}, value.rate)
            elif value.sbml_id == 'X_imm':
                self.assertEqual([1, 0], value.stoic)
                self.assertEqual({(0, 0): alpha}, value.rate)
            else:
                print value.sbml_id
                raise 'Unknown reaction'
        
    def testDimerModel(self):
        Variables().initialise()
        gdd = GetStoichiometryDict(sbml_file = 'xml_files/smfsb/single-dimer.xml')
        reaction_list = gdd.getReactionList()
        
        k1 =Variables().getParameter('k1')
        k2 = Variables().getParameter('k2')
        P0 = Variables().getParameter('P0')
        
        for value in reaction_list:

            if value.sbml_id == 'Dimerisation':
                self.assertEqual([-2], value.stoic)
                self.assertEqual(len(value.rate),2)
                exact = value.rate.get((2,),'X') - numeric(1)/numeric(2)*k1
                exact += value.rate.get((1,),'Y') + numeric(1)/numeric(2)*k1
                self.assertEqual(exact, 0)
            elif value.sbml_id == 'Dissociation':
                self.assertEqual([2], value.stoic)
                self.assertEqual(len(value.rate),2)
                print value.rate
                exact = value.rate.get((1,),'X') + numeric(1)/numeric(2)*k2
                exact += value.rate.get((0,),'Y') - numeric(1)/numeric(2)*k2*P0
                self.assertEqual(exact, 0)
            else:
                print value.sbml_id
                raise 'Unknown reaction'
        
    def testDummyModel(self):
        Variables().initialise()
        gdd = GetStoichiometryDict(sbml_file = 'xml_files/testReaction.xml')
        reaction_list = gdd.getReactionList()
        
        l =Variables().getParameter('lambda')
        m = Variables().getParameter('mu')

        
        for value in reaction_list:
            if value.sbml_id == 'Reaction1':
                self.assertEqual([-2, 0, 2, 0, 1], value.stoic)
                self.assertEqual(len(value.rate),1)


        
             
 
if __name__ == '__main__':
    unittest.main()









#ch07-dimer-stoch.xml
#LV2.xml                               
#ch07-mm-det.xml
#imdeath.xml                      
#immigration_migration_model.xml
#sis.xml
